package net.zhaolei.zhaolei_redis.controller;


import net.zhaolei.zhaolei_redis.dao.VideoDao;
import net.zhaolei.zhaolei_redis.model.VideoDO;
import net.zhaolei.zhaolei_redis.util.JsonData;
import net.zhaolei.zhaolei_redis.util.JsonUtil;
import net.zhaolei.zhaolei_redis.vo.CartItemVO;
import net.zhaolei.zhaolei_redis.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/v1/cart")
public class CartController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private VideoDao videoDao;

    @RequestMapping("add")
    public JsonData addCart(int videoId, int buyNum) {
        //get my cart
        BoundHashOperations<String, Object, Object> myCart = getCart();

        Object cacheObj = myCart.get(videoId+"");
        String result = "";
        if (cacheObj != null) {
            result = (String) cacheObj;
        }

        if (cacheObj == null) {
            CartItemVO cartItem = new CartItemVO();
            VideoDO videoDO = videoDao.findDetailById(videoId);

            cartItem.setBuyNum(buyNum);
            cartItem.setPrice(videoDO.getPrice());
            cartItem.setProductId(videoDO.getId());
            cartItem.setProductImg(videoDO.getImg());
            cartItem.setProductTitle(videoDO.getTitle());

            myCart.put(videoId+"", JsonUtil.objectToJson(cartItem));
        } else {
            // 增加商品的数量
            CartItemVO cartItemVO = JsonUtil.jsonToObject(result,CartItemVO.class);
            cartItemVO.setBuyNum(cartItemVO.getBuyNum() + buyNum);

            myCart.put(videoId+"", JsonUtil.objectToJson(cartItemVO));
        }

        return JsonData.buildSuccess();
    }

    @RequestMapping("mycart")
    public JsonData getMyCart() {
        //获取购物车，包含全部的feilds和values
        BoundHashOperations<String, Object, Object> myCart = getCart();

        List<Object> itemList = myCart.values();
        //把从redis里取出来的values反序列化为Object然后组装成list
        List<CartItemVO> cartItemVOList = new ArrayList<>();

        for (Object item : itemList) {
            CartItemVO cartItemVO = JsonUtil.jsonToObject((String) item, CartItemVO.class);
            cartItemVOList.add(cartItemVO);
        }

        CartVO cartVO = new CartVO();
        cartVO.setCartItems(cartItemVOList);
        //这里直接返回redis里保存的数据也是可以的，但是格式会有不同
        return JsonData.buildSuccess(cartItemVOList);
    }

    @RequestMapping("clear")
    public JsonData clear() {
        String key = getCartKey();
        redisTemplate.delete(key);

        return JsonData.buildSuccess();
    }

    private BoundHashOperations<String, Object, Object> getCart() {
        String key = getCartKey();

        return redisTemplate.boundHashOps(key);
    }

    private String getCartKey() {
        // user id, 可以从拦截器获取，引申问题：redis缓存和threadlocal之间有什么区别？？
        int userId = 88;
        String cartKey = String.format("video:cart:%s", userId);
        return cartKey;
    }
}
